package com.cskaoyan.shopping.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.api.OrderProductService;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.mall.dto.ClearCartItemRequest;
import com.cskaoyan.mall.dto.ClearCartItemResponse;
import com.cskaoyan.shopping.dto.*;
import com.cskaoyan.shopping.form.CartForm;
import com.cskaoyan.shopping.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Charadic
 * @since 2022/05/18 22:26
 */
@RestController
public class CartController{

    @Autowired
    ICartService cartService;

    /**
     * 添加商品到购物车
     *
     * @param cartForm
     * @return
     */
    @PostMapping("/shopping/carts")
    public ResponseData addProductToCart(@RequestBody CartForm cartForm) {

        AddCartRequest addCartRequest = new AddCartRequest();
        addCartRequest.setUserId(cartForm.getUserId());
        addCartRequest.setItemId(cartForm.getProductId());
        addCartRequest.setNum(cartForm.getProductNum());

        // 根据业务逻辑处理请求
        AddCartResponse addCartResponse = cartService.addToCart(addCartRequest);

        if (ShoppingRetCode.SUCCESS.getCode().equals(addCartResponse.getCode())) {
            return new ResponseUtil().setData("成功");
        }

        // 业务方法执行失败
        return new ResponseUtil().setErrorMsg(addCartResponse.getMsg());
    }

    /**
     * 获得购物车列表
     *
     * @param request
     * @return
     */
    @GetMapping("/shopping/carts")
    public ResponseData queryCart(HttpServletRequest request) {

        String userInfo = request.getHeader("user_info");
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.valueOf(object.get("uid").toString());

        CartListByIdRequest cartListByIdRequest = new CartListByIdRequest();
        cartListByIdRequest.setUserId(uid);

        CartListByIdResponse response = cartService.getCartListById(cartListByIdRequest);

        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            return new ResponseUtil().setData(response.getCartProductDtos());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    /**
     * 更新购物车中的商品
     *
     * @param cartForm
     * @return
     */
    @PutMapping("/shopping/carts")
    public ResponseData updateCart(@RequestBody CartForm cartForm) {

        UpdateCartNumRequest updateCartNumRequest = new UpdateCartNumRequest();
        updateCartNumRequest.setUserId(cartForm.getUserId());
        updateCartNumRequest.setItemId(cartForm.getProductId());
        updateCartNumRequest.setNum(cartForm.getProductNum());
        updateCartNumRequest.setChecked(cartForm.getChecked());

        UpdateCartNumResponse updateCartNumResponse = cartService.updateCartNum(updateCartNumRequest);

        if (ShoppingRetCode.SUCCESS.getCode().equals(updateCartNumResponse.getCode())) {
            return new ResponseUtil().setData("成功");
        }
        return new ResponseUtil().setErrorMsg(updateCartNumResponse.getMsg());
    }

    /**
     * 全选或者全不选商品
     * @param
     * @return
     * @author tangwei
     * @since 2022/5/19 10:58
     */
    @PutMapping("/shopping/items")
    public ResponseData checkAllCartItem(@RequestBody CheckAllItemRequest checkAllItemRequest) {

        CheckAllItemResponse checkAllItemResponse = cartService.checkAllCartItem(checkAllItemRequest);

        if (ShoppingRetCode.SUCCESS.getCode().equals(checkAllItemResponse.getCode())) {
            return new ResponseUtil().setData("成功");
        }

        // 业务方法执行失败
        return new ResponseUtil().setErrorMsg(checkAllItemResponse.getMsg());
    }

    /**
     * 删除购物车中选中的商品
     * @param
     * @return
     * @author tangwei
     * @since 2022/5/19 11:35
     */
    @DeleteMapping("/shopping/items/{uid}")
    public ResponseData deleteCheckedItem(@PathVariable("uid") Long uid) {
        DeleteCheckedItemRequest deleteCheckedItemRequest = new DeleteCheckedItemRequest();
        deleteCheckedItemRequest.setUserId(uid);

        DeleteCheckedItemResposne deleteCheckedItemResposne = cartService.deleteCheckedItem(deleteCheckedItemRequest);

        if (ShoppingRetCode.SUCCESS.getCode().equals(deleteCheckedItemResposne.getCode())) {
            return new ResponseUtil().setData("成功");
        }

        // 业务方法执行失败
        return new ResponseUtil().setErrorMsg(deleteCheckedItemResposne.getMsg());
    }

    /**
     * 删除购物车中的商品
     * @param
     * @return
     * @author tangwei
     * @since 2022/5/19 11:46
     */
    @DeleteMapping("/shopping/carts/{uid}/{pid}")
    public ResponseData deleteCartItem(@PathVariable("uid") Long uid, @PathVariable("pid") Long pid) {
        DeleteCartItemRequest deleteCartItemRequest = new DeleteCartItemRequest();
        deleteCartItemRequest.setUserId(uid);
        deleteCartItemRequest.setItemId(pid);

        DeleteCartItemResponse deleteCartItemResponse = cartService.deleteCartItem(deleteCartItemRequest);

        if (ShoppingRetCode.SUCCESS.getCode().equals(deleteCartItemResponse.getCode())) {
            return new ResponseUtil().setData("成功");
        }

        // 业务方法执行失败
        return new ResponseUtil().setErrorMsg(deleteCartItemResponse.getMsg());

    }

    // @Override
    // @RequestMapping(value = "/rpc/items",method = RequestMethod.DELETE)
    // public ClearCartItemResponse clearCartItemByUserID(ClearCartItemRequest request) {
    //
    //     ClearCartItemResponse clearCartItemResponse = cartService.clearCartItemByUserID(request);
    //     return clearCartItemResponse;
    // }
}
